package com.mcarthur.countdown.client.drawable;

import java.util.Iterator;
import java.util.Queue;
import java.util.Stack;

import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.gwtgl.binding.WebGLRenderingContext;
import com.mcarthur.countdown.client.images.Images;
import com.mcarthur.countdown.client.renderer.CountdownRenderer;

public class BlackHoleAnimator extends AbstractBallAnimator {
    //private static final int X = 1132;
    private Image blackHole;
    private Vector2 coords = new Vector2(0,0);
    private static final float MAGNITUDE = 0.005f;

    public BlackHoleAnimator() {
        this(null, null);
    }
    
    public BlackHoleAnimator(Queue<BouncyBall> activeBalls, Stack<BouncyBall> inactiveBalls) {
        super(activeBalls, inactiveBalls);
        blackHole = new Image(Images.INSTANCE.blackHole());
        blackHole.getElement().setId("black-hole");
        RootPanel.get().add(blackHole);
    }

    @Override
    public void draw(WebGLRenderingContext gl, long dt) {
        for (Iterator<BouncyBall> it = activeBalls.iterator(); it.hasNext();) {
            BouncyBall ball = it.next();

//            if (ball.getPosX() <= -Ball.WIDTH || ball.getPosX() >= CountdownRenderer.viewWidth) {
//                it.remove();
//                inactiveBalls.push(ball);
//                continue;
//            }

            //ball.setVelY(ball.getVelY() - (GRAVITY * dt));
            
            Vector2 ballPos = new Vector2(ball.getPosX(), ball.getPosY());
            Vector2 dir = coords.sub(ballPos).getUnit();
            dir = dir.mult(MAGNITUDE * dt);
            
            ball.setVelX(ball.getVelX() + dir.x);
            ball.setVelY(ball.getVelY() + dir.y);

            ball.setPosX(ball.getPosX() + (ball.getVelX() * dt));
            ball.setPosY(ball.getPosY() + (ball.getVelY() * dt));

            //checkBounds(ball);
            //checkCollision(ball);

            ball.draw(gl);
        }
        //collidedBalls.clear();
    }
    
    @Override
    public void stop() {
        RootPanel.get().remove(blackHole);
    }
}
